package tau_sadna.tau_map;

import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ResultLineView extends RelativeLayout {

	private static final int TEXT_ID = 2;
	private static final int IMAGE_ID = 3;

	public ResultLineView(MainActivity main, Result result) {
		super(main);
		LayoutParams imageParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		imageParams.addRule(ALIGN_BOTTOM, TEXT_ID);
		imageParams.addRule(ALIGN_TOP, TEXT_ID);
		imageParams.addRule(ALIGN_PARENT_LEFT);
		LayoutParams textParams = new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		textParams.addRule(RIGHT_OF,IMAGE_ID);
		textParams.setMargins(15, 0, 15, 0);
		ImageView image = new ImageView(main);
		String pic_name = result.getIconName();
		Drawable drawable;
		try {
			drawable = main.getResources().getDrawable((Integer) R.drawable.class.getField(pic_name).get(R.drawable.class));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			Log.w("ResultLineView","Cannot open result icon "+pic_name+". using marker_red");
			drawable = main.getResources().getDrawable(R.drawable.marker_red);
		}
		// TODO use result-specific image
		image.setImageDrawable(drawable);
		image.setLayoutParams(imageParams);
		image.setId(IMAGE_ID);
		TextView text = new TextView(main);
		if (MainActivity.language == MainActivity.HEBREW)
			text.setGravity(Gravity.RIGHT);
		else
			text.setGravity(Gravity.LEFT);
		text.setText(result.getName().getName());
		text.setTextSize(20);
		text.setId(TEXT_ID);
		text.setLayoutParams(textParams);
		addView(text);
		addView(image);
		setOnClickListener(new ResultButtonListener(main,result));
	}

}
